var express = require('express');
var router = express.Router();
var userModel = require("../modules/UserModel");
var cors = require('cors')
var corsOptions = {
  origin: 'http://192.168.1.149:1225',
  optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}

router.all('/login', function(req, res, next) {
	// 这是获取隐藏域
	subflag = req.body['subflag'];
	if(subflag == undefined){
		console.log('未登录')
		// 渲染login页面
		res.render('login',{});
	}else{
		console.log('已登录')
		// 最终的结果是链接跳转
		userModel.login(req,res);
	}
  
});
var dataSuccess = {
  status: '100', 
  msg: '登录成功',
  data: {
      userId: '20170113',
      userName: 'hgdqstudio',
      blog: 'http://hgdqstudio.online'
  }
};
var dataError = {
  status: '99', 
  msg: '用户名或密码错误'
};
// router.post('/log1',function (req, res, next) {
  router.post('/log1',function (req, res, next) {
    // res.header("Access-Control-Allow-Origin", "*");
    // res.header("Access-Control-Allow-Origin", "http://localhost:1225");
    // res.header("Access-Control-Allow-Headers", "X-Requested-With");
    // res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    // res.header("X-Powered-By",' 3.2.1')
    // res.header("Content-Type", "application/json;charset=utf-8");
  // 打印post请求的数据内容
  console.log(req.body);
  console.log(req.body.username);
  console.log(req.body.password);
  if (req.body.username == "hgdqstudio" && req.body.password == "123456") {
      res.end(JSON.stringify(dataSuccess));
  } else {
      res.end(JSON.stringify(dataError));
  }
});

var questions=[
{
  data:213,
  num:444,
  age:12
},
{
  data:456,
  num:678,
  age:13
}];

//写个接口123
router.get('/123',function(req,res){
  res.status(200),
  res.json(questions)
});
//http://localhost:1225/users/zhuce
// router.post('/zhuce',function(req,res){
//   // var nicheng = req.param('nicheng')
//   console.log(nicheng)
//   res.end(JSON.stringify(dataSuccess));
// })

// router.post('/zhuce/:id',function(req,res){
// 	console.log('999 = ' + req.params.id )
// 	res.send('注册2')
// })

// get 传参的方法 
// router.get('/zhuce',function(req,res){
//   var nicheng = req.query['nicheng']
//   console.log(nicheng)
//   // 1111
//   // url 尾部带参数
//   //  /users/zhuce?email=11&pwd=11&repwd=11&nicheng=111 200 3.184 ms - 7
//   res.send('注册3')
// })

// post的传参方法
router.post('/zhuce',function(req,res){
  var nicheng = req.body['nicheng']
  console.log('post == ' +nicheng)
  userModel.zhuce(req,res)
  //  post == 111111
  // res.send('注册5')
})




// /* GET users listing. */
// router.get('/', function(req, res, next) {
//   res.send('I am users');
// });

// router.post('/', function(req, res, next) {
//   res.send('users post请求');
//   console.log('users post请求')
// });

// router.all('/all', function(req, res, next) {
//   res.send('get post请求都可以');
//   console.log('get post请求')
// });

// router.get('/list', function(req, res, next) {
//   res.send('用户列表');
//   console.log('用户列表')
// });

// router.get('/file', function(req, res, next) {
//   res.sendFile('/Users/shuah/Node/segment/routes/in.html');
//   //此处路径为该项目的根磁盘下的路径开始，如果是windows，则去掉D：，E：之类的
// });



module.exports = router;
